package businesslogic.courseBl;

import java.rmi.Naming;
import java.util.ArrayList;

import po.coursePO.CoursePO;
import vo.courseVO.CourseVO;
import businesslogicservice.courseblservice.CourseBLService;
import dataservice.ResultMessage;
import dataservice.courseDataService.CourseDatabaseService;

public class Course implements CourseBLService {

	private String userID;
	private static final String ADDRESS_STRING = "localhost";

	public Course(String userID) {
		this.userID = userID;
	}

	@Override
	public ResultMessage addCourse(CourseVO newCourse) throws Exception {
		ResultMessage resultMessage = null;
		CoursePO coursePO = new CoursePO(newCourse.getCourseId(),
				newCourse.getCourseName(), newCourse.getTeacherID(),
				newCourse.getTeacherName(), newCourse.getCredit(),
				newCourse.getCoursePeriod(), newCourse.getClassroom(),
				newCourse.getAssistant(), newCourse.getCourseTime(),
				newCourse.getAttribute(), newCourse.getMaxStudent(),
				newCourse.getTerm(), newCourse.getDepartmentID(),
				newCourse.getBook(), newCourse.getIntroduction());
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			resultMessage = courseDatabaseService.insert(coursePO);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultMessage;
	}

	@Override
	public ResultMessage updateCourse(CourseVO courseVO) throws Exception {
		ResultMessage resultMessage = null;
		CoursePO coursePO = new CoursePO(courseVO.getCourseId(),
				courseVO.getCourseName(), courseVO.getTeacherID(),
				courseVO.getTeacherName(), courseVO.getCredit(),
				courseVO.getCoursePeriod(), courseVO.getClassroom(),
				courseVO.getAssistant(), courseVO.getCourseTime(),
				courseVO.getAttribute(), courseVO.getMaxStudent(),
				courseVO.getTerm(), courseVO.getDepartmentID(),
				courseVO.getBook(), courseVO.getIntroduction());
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			resultMessage = courseDatabaseService.update(coursePO);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultMessage;
	}

	@Override
	public CourseVO getCourse(String courseId) throws Exception {
		CourseVO courseVO = null;
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			CoursePO coursePO;
			coursePO = courseDatabaseService.find(courseId);
			CourseVO temp = new CourseVO(coursePO.getCourseId(),
					coursePO.getCourseName(), coursePO.getTeacherID(),
					coursePO.getTeacherName(), coursePO.getCredit(),
					coursePO.getCoursePeriod(), coursePO.getClassroom(),
					coursePO.getAssistant(), coursePO.getCourseTime(),
					coursePO.getAttribute(), coursePO.getMaxStudent(),
					coursePO.getTerm(), coursePO.getDepartmentID(),
					coursePO.getBook(), coursePO.getIntroduction());
			courseVO = temp;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseVO;
	}

	@Override
	public ArrayList<CourseVO> getAllCourse() throws Exception {
		ArrayList<CourseVO> courseVOlist = new ArrayList<CourseVO>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			ArrayList<CoursePO> coursePOs = courseDatabaseService.findAll();
			for (CoursePO coursePO : coursePOs) {
				CourseVO courseVO = new CourseVO(coursePO.getCourseId(),
						coursePO.getCourseName(), coursePO.getTeacherID(),
						coursePO.getTeacherName(), coursePO.getCredit(),
						coursePO.getCoursePeriod(), coursePO.getClassroom(),
						coursePO.getAssistant(), coursePO.getCourseTime(),
						coursePO.getAttribute(), coursePO.getMaxStudent(),
						coursePO.getTerm(), coursePO.getDepartmentID(),
						coursePO.getBook(), coursePO.getIntroduction());
				courseVOlist.add(courseVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseVOlist;
	}

	public ArrayList<CourseVO> getSelectableCourses() throws Exception {
		ArrayList<CourseVO> courseVOlist = new ArrayList<CourseVO>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			// change dedaosuoyoukexuanke
			ArrayList<CoursePO> coursePOs = courseDatabaseService
					.findSelectableCourses();
			for (CoursePO coursePO : coursePOs) {
				CourseVO courseVO = new CourseVO(coursePO.getCourseId(),
						coursePO.getCourseName(), coursePO.getTeacherID(),
						coursePO.getTeacherName(), coursePO.getCredit(),
						coursePO.getCoursePeriod(), coursePO.getClassroom(),
						coursePO.getAssistant(), coursePO.getCourseTime(),
						coursePO.getAttribute(), coursePO.getMaxStudent(),
						coursePO.getTerm(), coursePO.getDepartmentID(),
						coursePO.getBook(), coursePO.getIntroduction());
				courseVOlist.add(courseVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseVOlist;
	}

	public ArrayList<CourseVO> getSelectableCourse(String departmentID)
			throws Exception {
		ArrayList<CourseVO> courseVOlist = new ArrayList<CourseVO>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			// change dedaofenyuanxikexuanke
			ArrayList<CoursePO> coursePOs = courseDatabaseService
					.findSelectableCourse(departmentID);
			for (CoursePO coursePO : coursePOs) {
				CourseVO courseVO = new CourseVO(coursePO.getCourseId(),
						coursePO.getCourseName(), coursePO.getTeacherID(),
						coursePO.getTeacherName(), coursePO.getCredit(),
						coursePO.getCoursePeriod(), coursePO.getClassroom(),
						coursePO.getAssistant(), coursePO.getCourseTime(),
						coursePO.getAttribute(), coursePO.getMaxStudent(),
						coursePO.getTerm(), coursePO.getDepartmentID(),
						coursePO.getBook(), coursePO.getIntroduction());
				courseVOlist.add(courseVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseVOlist;
	}

	public ArrayList<CourseVO> getMyCourse() throws Exception {
		ArrayList<CourseVO> courseVOlist = new ArrayList<CourseVO>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			// change dedaofenyuanxikexuanke
			ArrayList<CoursePO> coursePOs = courseDatabaseService
					.findCourseByTeacherID(userID);
			for (CoursePO coursePO : coursePOs) {
				CourseVO courseVO = new CourseVO(coursePO.getCourseId(),
						coursePO.getCourseName(), coursePO.getTeacherID(),
						coursePO.getTeacherName(), coursePO.getCredit(),
						coursePO.getCoursePeriod(), coursePO.getClassroom(),
						coursePO.getAssistant(), coursePO.getCourseTime(),
						coursePO.getAttribute(), coursePO.getMaxStudent(),
						coursePO.getTerm(), coursePO.getDepartmentID(),
						coursePO.getBook(), coursePO.getIntroduction());
				courseVOlist.add(courseVO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseVOlist;
	}

	public ArrayList<String> getAllDepartments() throws Exception {
		ArrayList<String> departmentList = new ArrayList<String>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			departmentList = courseDatabaseService.findAllDepartments();
			System.out.println(departmentList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return departmentList;

	}

	public ArrayList<CourseVO> getCourseByDepartments(String departmentID)
			throws Exception {
		ArrayList<CourseVO> courseList = new ArrayList<CourseVO>();
		try {
			CourseDatabaseService courseDatabaseService = (CourseDatabaseService) Naming
					.lookup("rmi://" + ADDRESS_STRING + ":8888/courseData");
			ArrayList<CoursePO> coursePOs = courseDatabaseService
					.findCourseByDepartment(departmentID);
			for (CoursePO coursePO : coursePOs) {
				CourseVO courseVO = new CourseVO(coursePO.getCourseId(),
						coursePO.getCourseName(), coursePO.getTeacherID(),
						coursePO.getTeacherName(), coursePO.getCredit(),
						coursePO.getCoursePeriod(), coursePO.getClassroom(),
						coursePO.getAssistant(), coursePO.getCourseTime(),
						coursePO.getAttribute(), coursePO.getMaxStudent(),
						coursePO.getTerm(), coursePO.getDepartmentID(),
						coursePO.getBook(), coursePO.getIntroduction());
				courseList.add(courseVO);
			}
			System.out.println(courseList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseList;
	}

}
